package no.ntnu.fp.messages.impl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.MessageCode;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.utils.XmlParser;

public class RequestMeetingsForEmployeeMessage implements Message {
	private final Employee employee;
	
	public RequestMeetingsForEmployeeMessage(Employee employee) {
		this.employee = employee;
	}

	public RequestMeetingsForEmployeeMessage() {
		this(null);
	}
	
	public Employee getEmployee() {
		return employee;
	}

	@Override
	public MessageCode getMessageCode() {
		return MessageCode.REQUEST_MEETINGS_FOR_EMPLOYEE_MESSAGE;
	}

	@Override
	public Message fromXmlString(String xml) {
		Document doc = XmlParser.parseXmlString(xml);
		Employee employee = new Employee((Element)doc.getElementsByTagName("employee").item(0));
		return new RequestMeetingsForEmployeeMessage(employee);
	}

	@Override
	public String toString() {
		Document doc = XmlParser.getThreadLocalDocumentBuilder().newDocument();
		Element messageElement = doc.createElement("message");
		messageElement.appendChild(employee.toXml(doc));
		return getMessageCode() + XmlParser.convertXmlToString(messageElement);
	}
}
